package com.baoxin.chenxixc4coach.fragments.student.subject;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baoxin.chenxixc4coach.MyCallback;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.adapter.TrainStudentAdapter;
import com.baoxin.chenxixc4coach.bean.TrainStudent;
import com.baoxin.chenxixc4coach.dialog.TestArrangeDialog;
import com.baoxin.chenxixc4coach.fragments.TitleFragment;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class TrainStudentFragment extends TitleFragment implements TrainStudentAdapter.TrainStuClickListener {
    private int subject_type;// 1 2 3 4对应科目1234
    private RecyclerView recyclerView;
    private List<TrainStudent> studentList;
    private TrainStudentAdapter adapter;

    private Handler handler;

    @Override
    public String getTabTitle() {
        return "在培学员";
    }

    public void setSubject_type(int subject_type) {
        this.subject_type = subject_type;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && handler != null) {
            loadData();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_single_list, container, false);

        recyclerView = view.findViewById(R.id.student_recycler_view);
        studentList = new ArrayList<>();
        adapter = new TrainStudentAdapter(getContext(), this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        studentList = (List<TrainStudent>) msg.obj;
                        adapter.setData(studentList);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        request();

        return view;
    }

    //根据subject_type来获取数据
    private void request() {

        JSONObject body = new JSONObject();
        try {
            body.put("page", 1);
            body.put("limit", 10);
            body.put("subject", subject_type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            NetUtil.post(activity, Interfaces.URL_TRAINING_STUDENT, body.toString(), new MyCallback(Interfaces.URL_TRAINING_STUDENT, handler));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void showArrangeDialog(final TrainStudent student) {

        TestArrangeDialog testArrangeDialog = new TestArrangeDialog();
        Bundle bundle = new Bundle();
        //todo-----------虽然是list但是目前还只能安排单个
        final ArrayList<TrainStudent> trainStudents = new ArrayList<>();
        trainStudents.add(student);
        //待安排考试的学员
        bundle.putParcelableArrayList("trainStudents", trainStudents);
        testArrangeDialog.setArguments(bundle);
        testArrangeDialog.setArrangeDoneListener(new TestArrangeDialog.TestArrangeDoneListener() {
            @Override
            public void onTestArrangeSucceed() {//成功安排
                trainStudents.remove(student);
                adapter.notifyDataSetChanged();
                Toast.makeText(activity, "安排考试成功！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTestArrangeFailed() {
                Toast.makeText(activity, "安排考试成功！", Toast.LENGTH_SHORT).show();
            }
        });
        testArrangeDialog.show(getFragmentManager(), "");

    }

    public void loadData() {
        request();
    }
}
